package com.dlwx.firefighting.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.activity.ProductListActivity;
import com.dlwx.firefighting.adapter.HomeHotAdapter;
import com.dlwx.firefighting.adapter.HomePageAdapter;
import com.dlwx.firefighting.bean.BaseBean;
import com.dlwx.firefighting.bean.GoodsBean;
import com.dlwx.firefighting.bean.HomeBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.utils.glide.ImageLoaderComm;
import com.dlwx.firefighting.views.GridSpacingItemDecorationAll;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/15.
 */

public class AllClassifyFragment extends Fragment {

    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    Unbinder unbinder;
    private HomePageAdapter homePageAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private CommonHeader header;
    private Banner banner;
    private ImageView img_ad;
    private ImageView image_item1,image_item2,image_item3,image_item4;
    private TextView tv_item1,tv_item2,tv_item3,tv_item4;
    private ImageView ll_content1_0,ll_content1_1,ll_content1_2;
    private ImageView ll_content2_0,ll_content2_1,ll_content2_2,ll_content2_3;
    private RecyclerView classify_recyclerView;
    private ImageView img_item4_0,img_item4_1,img_item4_2;
    private RelativeLayout rl_project;
    private RelativeLayout rl_equipment;
    private RelativeLayout rl_new;
    private RelativeLayout rl_hot;
    private RelativeLayout rl_more;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_classify, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initBanner();
        getGoodsList();
        return view;
    }

    private void initView() {
        homePageAdapter = new HomePageAdapter(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        homePageAdapter.setDataList(new ArrayList<GoodsBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(homePageAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();
        recyclerView.addItemDecoration(divider);
        addHeader();
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);
    }

    private void addHeader() {
        header = new CommonHeader(getActivity(), R.layout.header_all_classify);
        banner = (Banner) header.findViewById(R.id.banner);
        img_ad = (ImageView) header.findViewById(R.id.img_ad);
        image_item1 = (ImageView) header.findViewById(R.id.image_item1);
        image_item2 = (ImageView) header.findViewById(R.id.image_item2);
        image_item3 = (ImageView) header.findViewById(R.id.image_item3);
        image_item4 = (ImageView) header.findViewById(R.id.image_item4);
        tv_item1 = (TextView) header.findViewById(R.id.tv_item1);
        tv_item2 = (TextView) header.findViewById(R.id.tv_item2);
        tv_item3 = (TextView) header.findViewById(R.id.tv_item3);
        tv_item4 = (TextView) header.findViewById(R.id.tv_item4);
        ll_content1_0 = (ImageView) header.findViewById(R.id.ll_content1_0);
        ll_content1_1 = (ImageView) header.findViewById(R.id.ll_content1_1);
        ll_content1_2 = (ImageView) header.findViewById(R.id.ll_content1_2);
//
        ll_content2_0 = (ImageView) header.findViewById(R.id.ll_content2_0);
        ll_content2_1 = (ImageView) header.findViewById(R.id.ll_content2_1);
        ll_content2_2 = (ImageView) header.findViewById(R.id.ll_content2_2);
        ll_content2_3 = (ImageView) header.findViewById(R.id.ll_content2_3);
//
        img_item4_0 = (ImageView) header.findViewById(R.id.img_item4_0);
        img_item4_1 = (ImageView) header.findViewById(R.id.img_item4_1);
        img_item4_2 = (ImageView) header.findViewById(R.id.img_item4_2);
        classify_recyclerView = (RecyclerView) header.findViewById(R.id.classify_recyclerView);

         rl_project= (RelativeLayout) header.findViewById(R.id.rl_project);
         rl_equipment= (RelativeLayout) header.findViewById(R.id.rl_equipment);
         rl_new= (RelativeLayout) header.findViewById(R.id.rl_new);
         rl_hot= (RelativeLayout) header.findViewById(R.id.rl_hot);
         rl_more= (RelativeLayout) header.findViewById(R.id.rl_more);
//
        lRecyclerViewAdapter.addHeaderView(header);
    }
    private void initBanner() {


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void getGoodsList() {
        DialogUtils.showProgressDlg(getActivity(), "");
        OkGo.<String>post(Constant.INDEX_GOOODSLIST)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        List<HomeBean> beans = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<BaseBean<HomeBean,HomeBean>>() {
                        }.getType());
                        final Intent intent=new Intent(getActivity(), ProductListActivity.class);
                        if(beans==null)return;
                        if(beans.size()>0){
                            for (int i = 0; i < beans.size(); i++) {
                                final HomeBean item = beans.get(i);
                                switch (i){
                                    case 0:
//                                        传播图，广告图。
                                        List<HomeBean.ContentBean> banners = item.getContent();
                                        if(banners!=null) {
                                            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
                                            banner.setOnBannerListener(new OnBannerListener() {
                                                @Override
                                                public void OnBannerClick(int position) {
                                                }
                                            });
                                            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                                            banner.setImageLoader(new ImageLoaderComm(new HomeBean.ContentBean()));
                                            banner.setImages(banners);
                                            banner.start();
                                        }
                                            Glide.with(getActivity()).load(banners.get(banners.size()-1).getImage_mobile()).into(img_ad);
                                        break;
                                    case 1:
                                        Glide.with(getActivity()).load(item.getIcon_url()).into(image_item1);
                                        tv_item1.setText(item.getName());
                                        List<HomeBean.ContentBean> content1 = item.getContent();
                                        Glide.with(getActivity()).load(content1.get(0).getProductImageBig()).into(ll_content1_0);
                                        Glide.with(getActivity()).load(content1.get(1).getProductImageBig()).into(ll_content1_1);
                                        Glide.with(getActivity()).load(content1.get(2).getProductImageBig()).into(ll_content1_2);
                                        rl_project.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                intent.putExtra("name",item.getName());
                                                intent.putExtra("id",item.getTid());
                                                startActivity(intent);
                                            }
                                        });
                                        break;
                                    case 2:
                                        Glide.with(getActivity()).load(item.getIcon_url()).into(image_item2);
                                        tv_item2.setText(item.getName());
                                        List<HomeBean.ContentBean> content2 = item.getContent();
                                        Glide.with(getActivity()).load(content2.get(0).getProductImageBig()).into(ll_content2_0);
                                        Glide.with(getActivity()).load(content2.get(1).getProductImageBig()).into(ll_content2_1);
                                        Glide.with(getActivity()).load(content2.get(2).getProductImageBig()).into(ll_content2_2);
                                        Glide.with(getActivity()).load(content2.get(3).getProductImageBig()).into(ll_content2_3);
                                        rl_equipment.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                intent.putExtra("name",item.getName());
                                                intent.putExtra("id",item.getTid());
                                                startActivity(intent);
                                            }
                                        });

                                        break;
                                    case 3:
                                        Glide.with(getActivity()).load(item.getIcon_url()).into(image_item3);
                                        tv_item3.setText(item.getName());
                                        List<HomeBean.ContentBean> content3 = item.getContent();
                                        HomeHotAdapter homeHotAdapter=new HomeHotAdapter(getActivity());
                                        homeHotAdapter.setDataList(content3);
                                        GridLayoutManager manager=new GridLayoutManager(getActivity(),3);
                                        manager.setOrientation(GridLayoutManager.VERTICAL);
                                        GridSpacingItemDecorationAll gridSpacingItemDecoration=new GridSpacingItemDecorationAll(2,20,true);
//                                        GridDividerItemDecoration gridDividerItemDecoration=new GridDividerItemDecoration(8, ContextCompat.getColor(getActivity(),R.color.white));
                                        classify_recyclerView.setLayoutManager(manager);
                                        classify_recyclerView.addItemDecoration(gridSpacingItemDecoration);
                                        classify_recyclerView.setHasFixedSize(true);
                                        classify_recyclerView.setAdapter(homeHotAdapter);
                                        rl_hot.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                intent.putExtra("name",item.getName());
                                                intent.putExtra("id",item.getTid());
                                                startActivity(intent);
                                            }
                                        });
                                        break;
                                    case 4:
                                        Glide.with(getActivity()).load(item.getIcon_url()).into(image_item4);
                                        tv_item4.setText(item.getName());
                                        List<HomeBean.ContentBean> content4 = item.getContent();
                                        Glide.with(getActivity()).load(content4.get(0).getProductImageBig()).into(img_item4_0);
                                        Glide.with(getActivity()).load(content4.get(1).getProductImageBig()).into(img_item4_1);
                                        Glide.with(getActivity()).load(content4.get(2).getProductImageBig()).into(img_item4_2);
                                        rl_new.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                intent.putExtra("name",item.getName());
                                                intent.putExtra("id",item.getTid());
                                                startActivity(intent);
                                            }
                                        });
                                        break;
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
